//
//  HMStatusPictureView.swift
//  MyWeibo
//
//  Created by apple on 16/4/22.
//  Copyright © 2016年 apple. All rights reserved.



//  创建一个collectionview来展示加载视图数据

import UIKit

//设置每个条目之间的间距
let HMStatusPictureItemMargin:CGFloat = 5
//设置每个item的宽高
let HMStatusPictureItemWH:CGFloat = {
    
    //类型转换
    let intW = Int((SCREENW - 2 * HMStatusCellMargin - 2 * HMStatusPictureItemMargin) / 3)
    
    let floatW = CGFloat(intW)
    
    return floatW
}()

class HMStatusPictureView: UICollectionView {

   private let pictureViewCellId = "pictureViewCellId"
    
    
    
    //元组类型 返回两个值
    var dataInfo: (pic_urls:[HMStatusPhotoInfo],size:CGSize)?{

        didSet{
        
        // 判断如果是1张图片，设置itemSize为配图视图的大小
        if dataInfo?.pic_urls.count == 1 {
            //拿到layout
            let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
            //设置itemsize
            layout.itemSize = dataInfo?.size ?? CGSizeZero
            
        }else{
            // 否则设置成正方形 HMStatusPictureItemWH
            let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
            layout.itemSize = CGSize(width: HMStatusPictureItemWH, height: HMStatusPictureItemWH)
        }

            //更新图片尺寸大小
        self.snp_updateConstraints { (make) -> Void in
            make.size.equalTo(dataInfo?.size ?? CGSizeZero)
            
        }

        reloadData()
        }
    }
    
    //当前配图控件显示的配图数据
//    var pic_urls: [HMStatusPhotoInfo]? {
//        didSet{
//            
//            label.text = "\(pic_urls?.count ?? 0)"
//            
//            // 刷新数据
//            reloadData()
//        }
//    }

    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   

    func setupUI(){
        
//        backgroundColor = UIColor.orangeColor()
        
        backgroundColor = UIColor(white: 242/255, alpha: 1)
        //注册cell
        registerClass(HMStatusPictureViewCell.self, forCellWithReuseIdentifier: pictureViewCellId)
        
        //设置每一个item的大小
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSize(width: HMStatusPictureItemWH, height: HMStatusPictureItemWH)
        
        //设置条目之间的间距
        layout.minimumInteritemSpacing = HMStatusPictureItemMargin
        layout.minimumLineSpacing = HMStatusPictureItemMargin
        

        self.dataSource = self
        
        addSubview(label)
        
        //设置label约束
        label.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }   
    }

    //懒加载一个label
    private lazy var label:UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)

}

extension HMStatusPictureView:UICollectionViewDataSource{
    
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataInfo?.pic_urls.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureViewCellId, forIndexPath: indexPath) as! HMStatusPictureViewCell
        
        cell.backgroundColor = RandomColor
        
        cell.photoInfo = dataInfo!.pic_urls[indexPath.item]
        
        return cell
    }
    
}

//自定义cell
class HMStatusPictureViewCell: UICollectionViewCell {
    
    //创建提供图片数据的属性
    var photoInfo:HMStatusPhotoInfo?{
        
        didSet{
            //给imageview赋值
            imageView.sd_setImageWithURL(NSURL(string: photoInfo?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_image_placeholder"))
            
            //判断设置gif是否显示
            gifImageView.hidden = !(photoInfo?.thumbnail_pic ?? "").hasSuffix("gif")
        }
        
    }
    
    
    //重写init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
        
        
        //添加控件
        contentView.addSubview(imageView)
        //添加gif控件
        contentView.addSubview(gifImageView)
        
        
        //设置约束
        imageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView)
        }
        //设置GIF约束
        gifImageView.snp_makeConstraints { (make) -> Void in
            
            make.trailing.bottom.equalTo(contentView)
            
        }
        
    }
    
    //懒加载控件
    private lazy var imageView:UIImageView = {
        
        let imageView = UIImageView (image: UIImage(named: "timeline_image_placeholder"))
        
        //设置内容模式
        imageView.contentMode = .ScaleAspectFill
        
        //切掉超出部分
        imageView.clipsToBounds = true
        
        return imageView
    }()
    //懒加载git控件
    private lazy var gifImageView : UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
}
